#include<iostream>
#include<unistd.h>
#include<sys/eventfd.h>
#include<pthread.h>
using namespace std;

int evfd=-1;
void *wait(void* args)
{
    string name=(const char*)args;
    while(1)
    {
        uint64_t flag=0;
        ssize_t n=::read(evfd,&flag,sizeof(flag));
        cout<<name<<"被唤醒...,flag: "<<flag<<":"<<errno<<endl;

    }
}
void *wakeup(void* args)
{
    string name=(const char*)args;
    while(1)
    {
        sleep(1);
        errno++;//errno每个线程一份
        cout<<"wake up one thread "<<errno<<endl;
        uint64_t flag=1;
        ::write(evfd,&flag,sizeof(flag));

    }
}
int main()
{
    evfd=::eventfd(0,0);
    pthread_t tid1,tid2;
    pthread_create(&tid1,nullptr,wait,(void*)"thread-1");
    pthread_create(&tid2,nullptr,wakeup,(void*)"thread-2");

    pthread_join(tid1,nullptr);
    pthread_join(tid2,nullptr);
    ::close(evfd);
    return 0;
}